/*
 * #%L
 * java model
 * %%
 * Copyright (C) 2009 - 2011 prefabware.com Stefan Isele
 * %%
 * All Rights Reserved.
 * This program is dual-licensed under the GNU General Public license
 * or a commercial license.
 * You may select to use one or the other of these licenses.
 * To obtain a commercial license please contact Stefan Isele at www.stefan-isele.de
 * Any modifications to this file must keep this entire header intact.
 * Redistribution, except as permitted by the selected license, is prohibited.
 * #L%
 */
package com.prefabware.jmodel.beans;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.prefabware.commons.CollectionUtil;
import com.prefabware.commons.Condition;
import com.prefabware.commons.QualifiedName;
import com.prefabware.commons.collection.Filter;
import com.prefabware.jmodel.JClass;
import com.prefabware.jmodel.JField;
import com.prefabware.jmodel.JMember;
import com.prefabware.jmodel.JType;
import com.prefabware.jmodel.JTypeDeclaration;

public class Bean extends JClass {
	public Bean(QualifiedName qn, JType[] typeArguments, JTypeDeclaration declaration) {
		super(qn, declaration);
		this.properties = new ArrayList<Property>();
	}

	private List<Property> properties;

	/**
	 * adds the property to this bean
	 * adds the properties field to this bean
	 * adds the properties getter to this bean
	 * adds the properties setter to this bean
	 * @param property
	 * @return
	 */
	public Bean add(Property property) {
		Condition.notNull(property, "..property");
		properties.add(property);
		if (property.getField()!=null ) {
			this.add(property.getField());
		}
		if (property.getGetter()!=null ) {
			this.add(property.getGetter());
		}
		if (property.getSetter()!=null) {
			this.add(property.getSetter());
		}
		return this;
	}

	public List<Getter> getGetter() {
		List<Getter> result = new ArrayList<Getter>();
		for (JMember method : this.getMethods()) {
			if (method instanceof Getter) {
				result.add((Getter) method);
			}
		}
		return Collections.unmodifiableList(result);
	}

	public Getter getGetter(final JField field) {
		Filter<? super Getter> filter=new Filter<Getter>() {
			
			@Override
			public boolean includes(Getter getter) {
				return getter.getField().equals(field);
			}
		};
		return CollectionUtil.first(this.getGetter(), filter);		
	}
	
	public List<Property> getProperties() {
		return Collections.unmodifiableList(this.properties);
	}
	
	public List<Setter> getSetter() {
		List<Setter> result = new ArrayList<Setter>();
		for (JMember method : this.getMethods()) {
			if (method instanceof Setter) {
				result.add((Setter) method);
			}
		}
		return Collections.unmodifiableList(result);
	}

	public Setter getSetter(final JField field) {
		Filter<? super Setter> filter=new Filter<Setter>() {

			@Override
			public boolean includes(Setter setter) {
				return setter.getField().equals(field);
			}
		};
		return CollectionUtil.first(this.getSetter(), filter);		
	}
}
